package com.hl.card.credit.pub.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hl.card.credit.pub.dao.AbstractDao;
import com.hl.card.credit.pub.dao.PublicOftenPDao;
import com.hl.card.credit.pub.entity.PublicOftenP;
import com.hl.card.credit.pub.entity.PublicOftenPCardInfo;
import com.hl.card.credit.pub.entity.PublicOftenPCertInfo;
import com.hl.card.credit.pub.model.PublicOftenPCardInfoModel;
import com.hl.card.credit.pub.model.PublicOftenPCertInfoModel;
import com.hl.card.credit.pub.model.PublicOftenPModel;
import com.hl.card.credit.pub.utils.BeanToMapUtils;
import com.hl.card.credit.pub.utils.CommonUtil;
import com.hl.card.credit.pub.utils.FormValid;
import com.hl.card.credit.pub.utils.StringUtils;
import com.hl.card.credit.pub.utils.UuidCodeUtils;

//@Service
public class PublicOftenPService extends AbstractService<PublicOftenP, Integer> {

//	@Autowired
	private PublicOftenPCardInfoService publicOftenPCardInfoService;
	
//	@Autowired
	private PublicOftenPCertInfoService publicOftenPCertInfoService;
	
	
//	@Autowired
	private PublicOftenPDao<PublicOftenP, Integer> dao;

	@Override
	protected AbstractDao<PublicOftenP, Integer> getDao() {
		return dao;
	}
	
	/**
	 * 
	    * @Title: queryOftenPList
	    * @Description: TODO(常旅客信息列表)
	    * @param @param publicOftenPModel
	    * @param @return    参数
	    * @return List<PublicOftenP>    返回类型
	    * @throws
	 */
	public List<PublicOftenP> queryOftenPList(PublicOftenPModel publicOftenPModel){
		return dao.queryOftenPList(publicOftenPModel);
	}
	
	
	/**
	 * 
	 * @Title: queryOftenPAllInfoList @Description:
	 * TODO(根据条件查询相关常旅客信息) @param @param publicOftenPModel @param @return
	 * 参数 @return List<Map<String,Object>> 返回类型 @throws
	 */
	public List<Map<String, Object>> queryOftenPAllInfoList(PublicOftenPModel publicOftenPModel) {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		List<PublicOftenP> oftenList = dao.queryOftenPList(publicOftenPModel);
		for (int i = 0; i < oftenList.size(); i++) {
			PublicOftenP publicOftenP = oftenList.get(i);
			if (CommonUtil.isNotEmpty(publicOftenP)) {
				if (CommonUtil.isNotEmpty(publicOftenP.getOften_p_code())) {
					Map<String, Object> map = BeanToMapUtils.toMap(publicOftenP);

					PublicOftenPCardInfoModel publicOftenPCardInfoModel = new PublicOftenPCardInfoModel();
					publicOftenPCardInfoModel.setOften_p_code(publicOftenP.getOften_p_code());
					List<PublicOftenPCardInfo> cardList = publicOftenPCardInfoService
							.queryOftenPCardInfoList(publicOftenPCardInfoModel);
					map.put("cardList", cardList);

					PublicOftenPCertInfoModel publicOftenPCertInfoModel = new PublicOftenPCertInfoModel();
					publicOftenPCertInfoModel.setOften_p_code(publicOftenP.getOften_p_code());
					List<PublicOftenPCertInfo> certList = publicOftenPCertInfoService
							.queryOftenPCertInfoList(publicOftenPCertInfoModel);
					map.put("certList", certList);
					list.add(map);
				}
			}
		}
		return list;
	}

	public PublicOftenP addAll(String param) {
		String code = UuidCodeUtils.uuid32();
		Map<String, Object> map = JSONObject.parseObject(param, Map.class);
		FormValid.validNull(map, "数据解析为空");
		JSONObject jsonObject =(JSONObject)map.get("certInfo");
		PublicOftenPCertInfo certInfo = jsonObject.toJavaObject(PublicOftenPCertInfo.class);
		System.out.println("certInfo:"+JSON.toJSONString(certInfo));
		FormValid.validNull(certInfo, "证件数据为空");
		FormValid.validNull(certInfo.getCert_name(), "姓名数据为空");
		FormValid.validNull(certInfo.getGender(), "证件号数据为空");

		jsonObject =(JSONObject)map.get("oftenP");
		PublicOftenP oftenP = jsonObject.toJavaObject(PublicOftenP.class);
		FormValid.validNull(oftenP, "数据为空");
		FormValid.validNull(oftenP.getMerchant_code(), "商户编号数据为空");
		FormValid.validNull(oftenP.getUser_identifying(), "用户账号数据为空");
		int add = 0;
		// 中文
		if (StringUtils.isNotEmpty(oftenP.getCn_name())) {
			PublicOftenPModel oftenPModel = new PublicOftenPModel();
			PublicOftenP oftenPTemp = dao.queryByCondition(oftenPModel);
			// 存在常旅客
			if (CommonUtil.isNotEmpty(oftenPTemp)) {
				PublicOftenPCertInfoModel certInfoModel = new PublicOftenPCertInfoModel();
				certInfoModel.setGender(certInfo.getGender());
				PublicOftenPCertInfo certInfoTemp = publicOftenPCertInfoService.queryByCondition(certInfoModel);
				// 不存在证件
				if (CommonUtil.isEmpty(certInfoTemp)) {
					// 添加证件
					add = 1;
					code = certInfoTemp.getOften_p_code();
					// publicOftenPCertInfoService.add(certInfo);
				}
			} else {// 不存在常旅客
					// 添加常旅客
					// 添加证件
				add = 2;
				// dao.add(oftenP);
				// publicOftenPCertInfoService.add(certInfo);
			}
		} else {// 空
			PublicOftenPCertInfoModel certInfoModel = new PublicOftenPCertInfoModel();
			certInfoModel.setGender(certInfo.getGender());
			System.out.println("certInfoModel:"+JSON.toJSONString(certInfoModel));
			PublicOftenPCertInfo certInfoTemp = publicOftenPCertInfoService.queryByCondition(certInfoModel);
			// 不存在
			if (CommonUtil.isEmpty(certInfoTemp)) {
				// 添加证件
				// 添加常旅客
				add = 2;
//				code = certInfoTemp.getOften_p_code();
				// publicOftenPCertInfoService.add(certInfo);
			}

		}
		oftenP.setOften_p_code(code);
		certInfo.setIs_effect(1);
		if (add == 2) {
			certInfo.setOften_p_code(code);

			dao.add(oftenP);
			publicOftenPCertInfoService.add(certInfo);
		} else if (add == 1) {
			dao.add(oftenP);
		}
		return oftenP;
	}

	public void updatePublicOftenP(String param) {
		// TODO Auto-generated method stub
		Map<String, Object> map = JSONObject.parseObject(param, Map.class);
		FormValid.validNull(map, "数据解析为空");

		JSONObject jsonObject = (JSONObject) map.get("certInfo");
		if (jsonObject != null) {

			PublicOftenPCertInfo certInfo = jsonObject.toJavaObject(PublicOftenPCertInfo.class);
			if (certInfo != null) {
				FormValid.validNull(certInfo.getOften_p_cert_id(), "often_p_cert_id为空");
				publicOftenPCertInfoService.updateBySelective(certInfo);
			}
		}
		jsonObject = (JSONObject) map.get("cardInfo");
		if (jsonObject != null) {
			PublicOftenPCardInfo cardInfo = jsonObject.toJavaObject(PublicOftenPCardInfo.class);
			if (cardInfo != null) {
				FormValid.validNull(cardInfo.getOften_p_card_id(), "often_p_card_id为空");
			}
		}
		jsonObject = (JSONObject) map.get("oftenP");
		if (jsonObject != null) {
			PublicOftenP oftenP = jsonObject.toJavaObject(PublicOftenP.class);
			if (oftenP != null) {
				FormValid.validNull(oftenP.getOften_p_code(), "often_p_code为空");
				updateBySelective(oftenP);
			}
		}

	}
}
